<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btncancel">
    <el-form
      :model="formData"
      label-width="120px"
      :rules="rules"
      ref="formData"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          autocomplete="off"
          style="width:50%"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="formData.mobile"
          autocomplete="off"
          style="width:50%"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <!-- 日期 -->
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          placeholder="请选择入职时间"
          v-model="formData.timeOfEntry"
          style="width: 40%;"
        ></el-date-picker>
      </el-form-item>
      <!-- 聘用形式 -->
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          placeholder="请选择聘用形式"
          style="width:50%"
        >
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <!-- 工号 -->
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          autocomplete="off"
          style="width:50%"
          placeholder="请输入工号"
        ></el-input>
      </el-form-item>

      <!-- 部门 -->
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          autocomplete="off"
          style="width:50%"
          placeholder="请选择部门"
          @focus="getDepartment"
        />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          default-expand-all=""
          :props="{ label: 'name' }"
          @node-click="selectNode"
        />
      </el-form-item>
      <!-- 转正时间 -->
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          placeholder="请选择转正时间"
          v-model="formData.correctionTime"
          style="width: 40%;"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="btnOk" type="primary">确定</el-button>
      <el-button @click="btncancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartment } from '@/api/department' // 员工部门结构
import { tranListToTreeData } from '@/utils/index'
import EmployeeEnum from '@/api/constant/employees' // 引入员工的枚举对象
import { addemp } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      EmployeeEnum,
      treeData: [], // 部门树形结构
      showTree: false, // 树形结构的显示隐藏
      loading: false, // 树形结构的进度条
      formData: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '长度在 3 到 5 个字符',
            trigger: 'change'
          }
        ],
        correctionTime: [
          { required: true, message: '转正时间', trigger: 'blur' }
        ]
      }
    }
  },
  // created () {
  //   this.getDepartment()
  // },
  methods: {
    async getDepartment () {
      this.showTree = true
      this.loading = true
      let res = await getDepartment()
      console.log(res)
      let list = res.data.depts
      this.treeData = tranListToTreeData(list, '')
      this.loading = false
    },
    selectNode (node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    // 保存添加员工
    async btnOk () {
      await this.$refs.formData.validate()
      let res = await addemp(this.formData)
      // console.log(res)
      this.$message.success('添加员工成功')
      let pageInfo = this.$parent.page
      let pageIngoTotal = (pageInfo.total += 1)
      let lastPage = Math.ceil(pageIngoTotal / pageInfo.size)
      this.$parent.page.page = lastPage
      // 告诉父组件修改数据
      this.$parent.getempList() // this.$parent 父组件实例
      this.$parent.showDialog = false
    },
    btncancel () {
      this.formData = {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      this.$refs.formData.resetFields() // 移出表单校验
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style></style>
